package com.cmict.data.form.api.util;


import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cmict.data.form.api.entity.dataobject.SysFormDO;
import com.cmict.data.form.api.entity.vo.SysFromDataColumnVo;
import java.util.Map;


/**
 * @description: 导入字段类型工具类
 * @author: qiaoyu
 * @create: 2022-07-08 22:57
 **/
public class TypeUtil {

    public static Boolean isreturn(SysFromDataColumnVo column, SysFormDO sysFormDO) {
		//for(SysFromDataColumnVo column : vo.getData()){
			if(column.getType().equals("varchar")){
				try{
					String.valueOf(column.getValue());
				}catch (Exception e){
					return false;
				}
			}
			if(column.getType().equals("int") || column.getType().equals("bigint") || column.getType().equals("float")){
                Boolean isDept = isColumndept(column,sysFormDO);
                Map<Object, Object> map = IsNullUtil.selectInForm(column.getName(),column.getValue(),sysFormDO);
                if(!isDept){
                    if(map.size()==0){
                        map.put("value",column.getValue());
                    }
                }
                try{
                    if(ObjectUtil.isNotEmpty(map)){
                        Integer.parseInt(String.valueOf(map.get("value")));
                    }
                }catch (Exception e){
                    return false;
                }
			}
            if(column.getType().equals("double") || column.getType().equals("decimal")){
                Map<Object, Object> map = IsNullUtil.selectInForm(column.getName(),column.getValue(),sysFormDO);
                if(map.size()==0){
                    map.put("value",column.getValue());
                }
                try{
                    if(ObjectUtil.isNotEmpty(map)){
                        Double.parseDouble(String.valueOf(map.get("value")));
                    }
				}catch (Exception e){
					return false;
				}
			}
			/*if(column.getType().equals("datetime") || column.getType().equals("date")){
				try{
					Date LocalDateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(column.getValue());
				}catch (Exception e){
					return false;
				}
				return true;
			}*/

		//}
		return true;
	}
    public static Boolean isreturnint(SysFromDataColumnVo column) {
        if (column.getType().equals("int")) {
            try {
                Integer.parseInt(column.getValue());
            } catch (Exception e) {
                return false;
            }
        }
        return true;
    }

    public static Boolean isColumndept(SysFromDataColumnVo columnName, SysFormDO sysFormDO) {
        JSONObject personData = JSONObject.parseObject(sysFormDO.getFormJsonUser());
        JSONArray fields = personData.getJSONArray("fields");
        if(ObjectUtil.isNotEmpty(fields)) {
            for (int i = 0; i < fields.size(); i++) {
                String field = fields.getJSONObject(i).getString("__vModel__");
                JSONObject config = fields.getJSONObject(i).getJSONObject("__config__");
                String tagIcon = config.getString("tagIcon");
                if(ObjectUtil.isNotEmpty(field) && "depart".equals(tagIcon)){
                    if(field.equals(columnName.getName())){
                        return true;
                    }
                }
            }
        }
        return false;
    }
}

